package yang.java8.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

public class Util {
	
	private static Random r = new Random();
	
	/**
	 * 顺序存放index的list
	 * @param size
	 * @return
	 */
	public static List<MyObject> generateMyObjectList(int size) {
		List<MyObject> ret = new ArrayList<>();
		while (size > ret.size()) {
			ret.add(new MyObject(ret.size(), "Str" + r.nextInt()));
		}
		return ret;
	}
	
	public static List<String> generateList(int size) {
		List<String> ret = new ArrayList<>();
		while (size > ret.size()) {
			ret.add("Str" + r.nextInt());
		}
		return ret;
	}
	
	public static List<Integer> generateIntList(int size) {
		List<Integer> ret = new ArrayList<>();
		while (size > ret.size()) {
			ret.add(r.nextInt());
		}
		return ret;
	}
	
	public static AtomicInteger[] generateAtomicIntList(int size, int defaultValue) {
		AtomicInteger[] ret = new AtomicInteger[size];
		for (int i = 0; i < size; i++) {
			ret[i] = new AtomicInteger(defaultValue);
		}
		return ret;
	}
	
	public static long showTime(long before, String key) {
		long now = System.currentTimeMillis();
		System.out.println(key + "  TimeAdd:" + (now - before));
		System.out.println(key + "  now:" + now);
		return now;
	}
	
	public static long showTime(long before) {
		return showTime(before, "");
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void showStream(Stream stream, int limit) {
		stream.peek(System.out::println).limit(limit).count();
	}
	
	public static void ShowArray(Object[] objs) {
		showStream(Stream.of(objs), objs.length);
	}
	
	public static void ShowList(List<? extends Object> list) {
		showStream(list.stream(), list.size());
	}
	
}
